﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Core.Packets
{
    public class DataPacket : Packet
    {
        private const int DATA_SIZE = 128;

        private byte[] data = new byte[DATA_SIZE];

        private DataPacket(byte numeroConnexion, byte typePacket, byte[] data)
        {
            this.numeroConnexion = numeroConnexion;
            this.typePacket = typePacket;
            this.data = data;
        }

        public byte[] Data { get { return data; } set { data = value; } }

        public static Packet[] CreateDataPackets(PDUSession PduSession, int prActuel, int psActuel)
        {
            //TODO s'assurer que les data remplisse les 128 bytes de données pour que le packet soit toujours de la même longueur
            int ctr = 0;
            byte[] array = Encoding.ASCII.GetBytes(PduSession.Data);
            Packet[] paquets = new Packet[(int)Math.Ceiling((double)array.Length / (double)128)];
            while(ctr < paquets.Length)
            {
                //paquets[ctr] = new DataPacket(Convert.ToByte(PduSession.ApplicationId), CreatePacketType(prActuel, true, psActuel), array);
            }
            return paquets;
        }

        //private static byte CreatePacketType(int pr, bool lastPacket, int ps)
        //{
        //    BitArray originalBitArray = new BitArray(8);
        //    string binary = Convert.ToString(pr, 2).PadLeft(3, '0');
        //    for (int ctr = 0; ctr < binary.Length; ctr++)
        //    {
        //        originalBitArray[ctr] = binary[ctr].ToString().Equals("1");
        //    }
        //    originalBitArray[3] = !lastPacket;
        //    binary = Convert.ToString(ps, 2).PadLeft(3, '0');
        //    for (int ctr = 4; ctr < binary.Length + 4; ctr++)
        //    {
        //        originalBitArray[ctr] = binary[ctr - 4].ToString().Equals("1");
        //    }
        //    originalBitArray[7] = false;
        //    return ConvertBitArrayToByte(originalBitArray);
        //}

        

        public void CreatePacketFromCharOfBitArray()
        {

        }

        public override string GetStringOfBitArray()
        {
            string arrayOfBits = string.Empty;
            foreach(byte dataByte in data)
            {
                arrayOfBits += Convert.ToString(dataByte, 2).PadLeft(8, '0');
            }
            return arrayOfBits;
        }
    }
}
